package entity;

import javax.swing.*;
import javax.swing.tree.*;
import storage.*;
import main.*;
import java.awt.event.*;

public class DatabaseTree extends JTree implements ActionListener
{
	JPopupMenu rootPopup;
    JPopupMenu databasePopup;
    JPopupMenu tablePopup;
    JPopupMenu columnPopup;
    JMenuItem mi;
    ManagementFrame parentFrame;
    DataAccessManager data;
    
    public DatabaseTree(ManagementFrame parentFrame) 
    {
        this.parentFrame = parentFrame;
        data = new DataAccessManager();
		setShowsRootHandles(true);
		putClientProperty("JTree.lineStyle", "None");
		
		rootPopup = new JPopupMenu();
        mi = new JMenuItem("New database");
        mi.addActionListener(this);
        mi.setActionCommand("newDatabase");
        rootPopup.add(mi);
        rootPopup.setOpaque(true);
        rootPopup.setLightWeightPopupEnabled(true);
        
        databasePopup = new JPopupMenu();
        mi = new JMenuItem("Rename database");
        mi.addActionListener(this);
        mi.setActionCommand("renameDatabase");
        databasePopup.add(mi);
        
        mi = new JMenuItem("Delete database");
        mi.addActionListener(this);
        mi.setActionCommand("deleteDatabase");
        databasePopup.add(mi);
        
        mi = new JMenuItem("New table");
        mi.addActionListener(this);
        mi.setActionCommand("newTable");
        databasePopup.add(mi);
        databasePopup.setOpaque(true);
        databasePopup.setLightWeightPopupEnabled(true);
        
        tablePopup = new JPopupMenu();
        mi = new JMenuItem("Rename table");
        mi.addActionListener(this);
        mi.setActionCommand("renameTable");
        tablePopup.add(mi);
        
        mi = new JMenuItem("Delete table");
        mi.addActionListener(this);
        mi.setActionCommand("deleteTable");
        tablePopup.add(mi);
        
        mi = new JMenuItem("New column");
        mi.addActionListener(this);
        mi.setActionCommand("newColumn");
        tablePopup.add(mi);

        mi = new JMenuItem("Edit data");
        mi.addActionListener(this);
        mi.setActionCommand("editData");
        tablePopup.add(mi);
        
        mi = new JMenuItem("Modify primary key");
        mi.addActionListener(this);
        mi.setActionCommand("modifyPrimaryKey");
        tablePopup.add(mi);
        tablePopup.setOpaque(true);
        tablePopup.setLightWeightPopupEnabled(true);
        
        columnPopup = new JPopupMenu();
        mi = new JMenuItem("Edit column");
        mi.addActionListener(this);
        mi.setActionCommand("editColumn");
        columnPopup.add(mi);
        
        mi = new JMenuItem("Delete column");
        mi.addActionListener(this);
        mi.setActionCommand("deleteColumn");
        columnPopup.add(mi);
        columnPopup.setOpaque(true);
        columnPopup.setLightWeightPopupEnabled(true);
        
        addMouseListener(new MouseAdapter() 
        {
            public void mouseReleased( MouseEvent e ) 
            {
                if (e.isPopupTrigger()) 
                {
                	TreePath path = getSelectionPath();
                	if (path!=null) switch (path.getPathCount())
                	{
	                	case 1:
	                		rootPopup.show( (JComponent)e.getSource(), e.getX(), e.getY() );
	                		break;
	                	case 2:
	                		databasePopup.show( (JComponent)e.getSource(), e.getX(), e.getY() );
	                		break;
	                	case 3:
	                		tablePopup.show( (JComponent)e.getSource(), e.getX(), e.getY() );
	                		break;
	                	case 4:
	                		columnPopup.show( (JComponent)e.getSource(), e.getX(), e.getY() );
	                		break;
                	}
                }
                else if (e.getClickCount()==2)
        		{
                	TreePath path = getSelectionPath();
                	if (path!=null) switch (path.getPathCount())
        			{
            			case 2:
                			renameDatabase();
                			break;
                		case 3:
                			editData();
                			break;
                		case 4:
                			editColumn();
                			break;
        			}
        		}
            }
        }
        );
        
    }
    
    public void newDatabase()
    {
    	CreateDatabaseDialog dialog = new CreateDatabaseDialog(parentFrame);
    }
    
    public void renameDatabase()
    {
    	TreePath path = this.getSelectionPath();
    	DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getPathComponent(1);
    	RenameDatabaseDialog dialog = new RenameDatabaseDialog(node.toString(), parentFrame);
    }
    
    public void deleteDatabase()
    {
    	TreePath path = this.getSelectionPath();
    	DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getPathComponent(1);
    	data.deleteDatabaseByName(node.toString());
    	parentFrame.refreshDatabaseTree();
    }
    
    public void newTable()
    {
    	TreePath path = this.getSelectionPath();
     	DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getPathComponent(1);
     	CreateTableDialog dialog = new CreateTableDialog(node.toString(),parentFrame);
    }
    
    public void renameTable()
    {
    	TreePath path = this.getSelectionPath();
     	DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getPathComponent(1);
     	DefaultMutableTreeNode tableNode = (DefaultMutableTreeNode) path.getPathComponent(2);
     	RenameTableDialog dialog = new RenameTableDialog(node.toString(),tableNode.toString(),parentFrame);
    }
    
    public void deleteTable()
    {
    	TreePath path = this.getSelectionPath();
     	DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getPathComponent(1);
     	DefaultMutableTreeNode tableNode = (DefaultMutableTreeNode) path.getPathComponent(2);
     	data.getDatabaseByName(node.toString()).removeTableByName(tableNode.toString());
     	data.saveDatabases();
     	parentFrame.refreshDatabaseTree();
    }
    
    public void newColumn()
    {
    	TreePath path = this.getSelectionPath();
    	DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getPathComponent(1);
    	DefaultMutableTreeNode tableNode = (DefaultMutableTreeNode) path.getPathComponent(2);
    	CreateColumnDialog dialog = new CreateColumnDialog(node.toString(),tableNode.toString(),parentFrame);
    }
    
    public void editColumn()
    {
    	TreePath path = this.getSelectionPath();
    	DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getPathComponent(1);
    	DefaultMutableTreeNode tableNode = (DefaultMutableTreeNode) path.getPathComponent(2);
    	DefaultMutableTreeNode columnNode = (DefaultMutableTreeNode) path.getPathComponent(3);
    	
    	Column column = data.getDatabaseByName(node.toString()).getTableByName(tableNode.toString()).getColumnByName(columnNode.toString());
    	EditColumnDialog dialog = new EditColumnDialog(node.toString(), tableNode.toString(), column.getName(), column.getType(), parentFrame);
    }
    
    public void deleteColumn()
    {
    	TreePath path = this.getSelectionPath();
    	DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getPathComponent(1);
    	DefaultMutableTreeNode tableNode = (DefaultMutableTreeNode) path.getPathComponent(2);
    	DefaultMutableTreeNode columnNode = (DefaultMutableTreeNode) path.getPathComponent(3);
    	
    	data.getDatabaseByName(node.toString()).getTableByName(tableNode.toString()).removeColumnByName(columnNode.toString());
    	data.saveDatabases();
    	parentFrame.refreshDatabaseTree();
    }
    
    public void editData()
    {
    	TreePath path = this.getSelectionPath();
    	DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getPathComponent(1);
    	DefaultMutableTreeNode tableNode = (DefaultMutableTreeNode) path.getPathComponent(2);
    	
    	parentFrame.addDataTab(node.toString(),tableNode.toString());
    }
    
    public void modifyPrimaryKey()
    {
    	TreePath path = this.getSelectionPath();
    	DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getPathComponent(1);
    	DefaultMutableTreeNode tableNode = (DefaultMutableTreeNode) path.getPathComponent(2);
    	
    	ChoosePrimaryKeyDialog dialog = new ChoosePrimaryKeyDialog(node.toString(), tableNode.toString(), parentFrame);
    }
    public void actionPerformed(ActionEvent ae) 
    {
        if (ae.getActionCommand().equals("newDatabase"))  newDatabase();
        if (ae.getActionCommand().equals("renameDatabase")) renameDatabase();
        if (ae.getActionCommand().equals("deleteDatabase"))  deleteDatabase();
        if (ae.getActionCommand().equals("newTable"))  newTable();
        if (ae.getActionCommand().equals("renameTable"))  renameTable();
        if (ae.getActionCommand().equals("deleteTable")) deleteTable();
        if (ae.getActionCommand().equals("newColumn")) newColumn();
        if (ae.getActionCommand().equals("editColumn")) editColumn();
        if (ae.getActionCommand().equals("deleteColumn")) deleteColumn();
        if (ae.getActionCommand().equals("editData")) editData();
        if (ae.getActionCommand().equals("modifyPrimaryKey")) modifyPrimaryKey();
    }
}
